//
//  PhotoClass.m
//  UI10--HomeWork
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PhotoClass.h"
#import "UIImageView+WebCache.h"
@implementation PhotoClass
-(void)dealloc {
    [_arrayTitle release];
    [_array release];
    [super dealloc];
}


-(instancetype) initWithFrame:(CGRect)frame withArray:(NSArray *)array  withArrayTitle:(NSArray *)arrayTitle{
    self = [super initWithFrame:frame];
    if (self) {
        _array = [[NSMutableArray alloc] initWithArray:array];
        _arrayTitle = [[NSMutableArray alloc] initWithArray:arrayTitle];
    
        self.backgroundColor = [UIColor whiteColor];
        
        [self createScollView];
        
        
    }
    return self;
}

-(void)createScollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.tag = 1001;
    if (self.array.count) {
        
        [_array insertObject:[self.array lastObject] atIndex:0];
        [_arrayTitle insertObject:[self.arrayTitle lastObject] atIndex:0];
        [_array addObject:self.array[1]];
        [_arrayTitle addObject:self.arrayTitle[1]];
        
        for (int i = 0; i < _array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - 20)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_array[i]]];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;

            
            [scrollView addSubview:imageView];
            [imageView release];

            
        }
    }
    scrollView.contentSize = CGSizeMake(self.frame.size.width * _array.count, 0);
    scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    scrollView.delegate = self;

    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width * 3 / 4 - 30, self.frame.size.height - 20, self.frame.size.width / 4 + 30, 20)];
    page.tag = 1002;
    page.numberOfPages = _array.count - 2;
    [page addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    page.currentPageIndicatorTintColor = [UIColor blackColor];
    page.pageIndicatorTintColor = [UIColor grayColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width * 3 / 4 - 30, 20)];
    
    label.tag = 1003;
    label.font = [UIFont systemFontOfSize:13];
    label.text = _arrayTitle[1];
    
    [self addSubview:scrollView];
    [self addSubview:page];
    [self addSubview:label];
    
    [label release];
    [page release];
    [scrollView release];
    
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (self.array.count - 2), 0);
    }
    if (scrollView.contentOffset.x == self.frame.size.width *(self.array.count - 1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
    UIPageControl *pag = (UIPageControl *)[self viewWithTag:1002];
    pag.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    
    UILabel *label = (UILabel *)[self viewWithTag:1003];
    label.text = _arrayTitle[pag.currentPage + 1];
}

- (void)pageControlChange:(UIPageControl *)sender {

    UIScrollView *scroll = (UIScrollView *) [self viewWithTag:1001];
    [scroll setContentOffset:CGPointMake(scroll.frame.size.width * (sender.currentPage + 1), 0) animated:YES];
    
    UILabel *label = (UILabel *)[self viewWithTag:1003];
    label.text = _arrayTitle[sender.currentPage + 1];
}



@end
